package com.sunxd.rule.judge.spi;

import com.google.common.collect.Maps;
import com.sunxd.rule.judge.beans.param.Condition;
import com.sunxd.rule.judge.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.function.Function;

/**
 * @author sun.xd
 * @description: 自定义实现方式
 * @date 2024/10/22 11:32
 */
@Slf4j
public abstract class AbstractCustomerJudge implements CustomerJudge {

    private static final Map<String, Function<Condition, Boolean>> JUDGE = Maps.newHashMap();

    public AbstractCustomerJudge() {
        JUDGE.put(invokeKey(), doJudge());
    }

    public abstract Function<Condition, Boolean> doJudge();

    public static Boolean judge(Condition condition) {
        if (StringUtils.isBlank(condition.getInvokeKey()) || !JUDGE.containsKey(condition.getInvokeKey())) {
            log.error("The specified class was not found => invokeKey : {}", condition.getInvokeKey());
            return Boolean.FALSE;
        }
        return JUDGE.get(condition.getInvokeKey()).apply(condition);
    }

}
